/**
 * Created by Administrator on 2015/4/23.
 */
var onInputFileChange = {
	AUDIO_TYPE : ["mp3","wma","aac"],
	audio : function(targetInp){
		var types = this.AUDIO_TYPE;
		var allowToUpload = false;
		var btn = $("#uploadAudioBtn");
		var btnOrgTxt = btn.data("orignTxt");
		var filename = $(targetInp).val();
		if(!filename) return;
		var filetype = filename.substring(filename.indexOf(".")+1).toLowerCase();
		for(var i=0; i<types.length; i++){
			var type = types[i];
			if(type==filetype){
				allowToUpload = true;
				break;
			}
		}
		if(!allowToUpload){
			alert("请上传正确的音频格式文件");
		}else{
			if(!btnOrgTxt){
				btnOrgTxt = btn.html();
				btn.data("orignTxt",btnOrgTxt);
			}
			btn.html("正在上传...");
			$("input[name=upload_mth]").val("audio");
			$("#mainForm").submit();
		}
	}
};
//上传后成功事件回调
var uploadSuccess = {
	//音频
	audio : function(status,msg){
		if(status=="success" && msg){
			//alert("上传成功,文件地址是："+src);
			PFT_GLOBAL.U.Alert("success",'<p style="width:180px;">上传成功</p>');
			var btn = $("#uploadAudioBtn");
			btn.html(btn.data("orignTxt"));
			$("#tryListen").removeClass("disabled").attr("data-src",msg);
		}else{
			PFT_GLOBAL.U.Alert("fail",'<p style="width:180px;">'+msg+'</p>');
		}
	},
	//视频
	video : function(status,msg){
		$("#uploadTipVideo").hide();
		if(status=="success" && msg){
			$("#upfile_video_v").val(msg);
			$("#hasLoadedFlag_video").show().find(".abtn").attr("href",msg)
			PFT_GLOBAL.U.Alert("success",'<p style="width:280px;">上传视频成功</p>');
		}else{
			PFT_GLOBAL.U.Alert("fail",'<p style="width:280px;">'+msg+'</p>');
		}
	},
	//图片
	image : function(status,msg){
		$("#uploadTipImg").hide();
		if(status=="success" && msg){
			$("#thumb_img").val(msg);
			$("#upload_img_res_img").attr("src",msg);
			$("#uploadTipImg").hide();
			PFT_GLOBAL.U.Alert("success",'<p style="width:280px;">上传图片成功</p>');
		}else{
			PFT_GLOBAL.U.Alert("fail",'<p style="width:280px;">'+msg+'</p>');
		}
	},
	//360全景
	land360 : function(status,msg){
		$("#uploadTipLand360").hide();
		if(status=="success" && msg){
			$("#upfile_allvideo").val(msg);
			$("#hasLoadedFlag_land360").show().find(".abtn").attr("href",msg)
			$("#uploadTipLand360").hide();
			PFT_GLOBAL.U.Alert("success",'<p style="width:280px;">上传360全景成功</p>');
		}else{
			PFT_GLOBAL.U.Alert("fail",'<p style="width:280px;">'+msg+'</p>');
		}
	}
};

var TxtTransToAudio = (function(){
	var session = new Session({
		'url' : 'http://webapi.openspeech.cn/ivp',
		'interval' : '30000',
		'disconnect_hint' : 'disconnect',
		'sub' : 'tts'
	});
	//应用APPID，在open.voicecloud.cn上申请即可获得
	var appid = '5535bd08';
	//签名失效时间，单位:ms，例60000
	var expires = 60000;

	function tranTo(text,callbacks){
		var callbacks = callbacks || {};
		var fn = new Function;
		var begin = callbacks.begin || fn;
		var complete = callbacks.complete || fn;
		var error = callbacks.error || fn;
		var success = callbacks.success || fn;
		if(!text){
			console && console.log("没有待转化的文字");
			return false;
		}
		//当前时间戳，例new Date().toLocaleTimeString()
		var timestamp   = document.getElementById("timestamp").value;
		var signature = document.getElementById('sinature').value;
		var params = {
			"params" : "aue = speex-wb;7, ent = intp65, spd = 50, vcn=xiaoyu, vol = 50, tte = utf8, caller.appid="
			+ appid + ",timestamp=" + timestamp
			+ ",expires=" + expires, "signature" : signature, "gat" : "mp3"
		};
		var audioSrc = "";
		begin();
		session.start('tts',params,text,function(err,obj){
			complete(err);
			if(err){
				error(err);
			}else{
				audioSrc = "http://webapi.openspeech.cn/" + obj.audio_url;
				success(audioSrc);
			}
		});
	}
	return{
		tranTo : tranTo
	}
})();

$(function(){
	//点击按钮  合成语音
	$("#transToAudioBtn").on("click",function(e){
		var text = $("#detailInfo").val();
		var btn = $(e.target);
		var listenBtn = $("#tryListen");
		var orgBtnTxt = btn.html();
		if(!text) text = "鼓浪屿，海上明珠，岛上四季鲜花竞开，海岸曲折逶迤，岩石起伏有致，各式建筑错落点缀，深巷琴音隐约，享有“海上花园”、“钢琴之岛”、“万国建筑博览”、“中国最美城区”等诸多美誉。郑成功、弘一法师、鲁迅、马约翰、林语堂、林巧稚等众多名人在此留下身影。时间的风雨锤洗，赋予了鼓浪屿中西文化交汇、自然景观与人文景观交融的深厚内涵。";
		if(btn.hasClass("loading")) return false;
		PFT_GLOBAL.G.Ajax({
			url : "module/tts/TTSService.php",
			type : "post",
			dataType : "json",
			data : {
				text : text
			},
			ttiemout : 5 * 60 * 1000,
			loading : function(){
				btn.addClass("loading").html('<i class="iconfont loading">&#xe644;</i><span class="t">正在转成语音...</span>');
			},
			removeLoading : function(){ btn.removeClass("loading").html('<i class="iconfont">&#xe657;</i>转成语音并上传');},
			timeout : function(){
				PFT_GLOBAL.U.Alert("fail",'<p style="width:250px;">文字合成语音超时，请稍后重试</p>');
			},
			serverError : function(){
				PFT_GLOBAL.U.Alert("fail",'<p style="width:250px;">文字合成语音失败，请稍后重试</p>');
			}
		},function(res){
			if(res.status=="ok"){
				PFT_GLOBAL.U.Alert("success",'<p style="width:180px;">语音合成成功</p>');
				$("input[name=upfile_music_v]").val(res.url);
				listenBtn.removeClass("disabled").attr("data-src",res.url);
			}else{
				PFT_GLOBAL.U.Alert("fail",'<p style="width:250px;">res.msg</p>');
			}
		})
		//合成语音
//		TxtTransToAudio.tranTo(text,{
//			begin : function(){
//				btn.addClass("loading").html('<i class="iconfont"></i><span class="t">正在转成语音...</span>');
//			},
//			complete : function(error){
//				btn.removeClass("loading");
//			},
//			error : function(error){
//				alert("语音合成发生错误，错误代码 ：" + error);
//				btn.html("重 试");
//			},
//			success : function(src){
//				listenBtn.removeClass("disabled").data("src",src);
//				btn.html(orgBtnTxt);
//				PFT_GLOBAL.U.Alert("success",'<p style="width:180px;">成功语音合成</p>');
//				PFT_GLOBAL.G.Ajax({
//					url : "call/files_move.php",
//					type : "post",
//					dataType : "json",
//					data : { url : src},
//					loading : function(){ console && console.log("合成音频成功，正在上传音频链接地址...")},
//					timeout : function(){ console && console.log("合成音频成功，上传音频链接地址时上传超时")},
//					serverError : function(){ console && console.log("合成音频成功，上传音频链接地址时上传出错")}
//				},function(res){
//					if(res.status=="success"){
//						console && console.log("上传音频键接地址成功,新的链接地址为:"+res.link);
//						$("input[name=upfile_music_v]").val(res.link);
//					}else if(res.status="failure"){
//						console && console.log("上传音频键接地址失败");
//					}
//				})
//			}
//		});
		return false;
	})
	//播放语音
	var listenBtn = $("#tryListen");
	var btn_orign_html = listenBtn.html();
	var audioSrc = listenBtn.attr("data-src");
	if(audioSrc) listenBtn.removeClass("disabled");
	listenBtn.on("click",function(e){
		var btn = $(this);
		var src = btn.attr("data-src");
		if(btn.hasClass("disabled") || btn.hasClass("loading") || !src) return false;
		var audio = btn.data("audio");
		if(!audio){
			audio = new Audio();
			audio.onload = function(e){
				listenBtn.removeClass("playing").removeClass("pause").addClass("loading").html('<i class="iconfont loading">&#xe644;</i>加载中..');
			}
			audio.onprogress = function(){
				listenBtn.removeClass("loading").removeClass("pause").addClass("playing").html('<i class="iconfont loading">&#xe600;</i>播放中..');
			}
			audio.onplaying = function(){
				listenBtn.removeClass("loading").removeClass("pause").addClass("playing").html('<i class="iconfont loading">&#xe600;</i>播放中..');
			}
			audio.onpause = function(){
				listenBtn.removeClass("loading").removeClass("playing").addClass("pause").html('<i class="iconfont loading">&#xe600;</i>暂停中');
			}
			audio.onended = function(){
				listenBtn.removeClass("loading").removeClass("pause").removeClass("playing").html(btn_orign_html);
			}
			btn.data("audio",audio);
		}
		if(btn.hasClass("playing")){
			audio.pause();
			return false;
		}
		if(btn.hasClass("pause")){
			audio.play();
			return false;
		}
		audio.src=src;
		audio.play();
		return false;
	});
	if(audioSrc) listenBtn.removeClass("disabled");
	//音频
	$("#uploadAudioBtn").on("click",function(e){
		if($("#wrapper").hasClass("lt-ie9")){
			$("#daoyouAudio").trigger("click");
		}
	})
	//点击图片上传按钮
	$("#BtnUpload").bind('click',function(){
		if(!$("#imgup").val().length) return;
		$("#upload_mth").val("image");
		$("#uploadTipImg").show();
		$("form#mainForm").submit();
		$("#waitingUpload").html('<img src="images/loading.gif" alt="图片上传中"/>图片上传中..').show();
	});

	//点击视频上传按钮
	$("#BtnUpload2").bind('click',function(){
		if(!$("input#imgup2").val().length) return;
		$("#upload_mth").val("video");
		$("#uploadTipVideo").show();
		$("form#mainForm").submit();
		$("#waitingUpload2").html('<img src="images/loading.gif" alt="视频上传中"/>视频上传中..').show();
	});
	//点击360全景上传按钮
	$("#BtnUpload3").bind('click',function(){
		if(!$("input#imgup3").val().length) return;
		$("#upload_mth").val("land360");
		$("#uploadTipLand360").show();
		$("form#mainForm").submit();
		$("#waitingUpload3").html('<img src="images/loading.gif" alt="360全景上传中"/>360全景上传中..').show();
	});

	//上传360全景时先判断上传的文件名不能包含中文字符
	$("#imgup3").on("change",function(e){
		var filename = e.target.value;
		if(!filename) return false;
		var patrn=/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi;
		if(patrn.exec(filename)){
			alert("上传文件的文件名不能含有中文");
			e.target.value = "";
		}
	})


	var hasUpload_video = $("#hasLoadedFlag_video").find(".abtn").attr("href");
	var hasUpload_360Land = $("#hasLoadedFlag_land360").find(".abtn").attr("href");

	hasUpload_video && $("#hasLoadedFlag_video").show();
	hasUpload_360Land && $("#hasLoadedFlag_land360").show();

});

//将地址转成经纬度 2015-05-11新增
//author: 348845730@qq.com
$(window).on("load",function(){
	var addressInp = $("#text_b");
	var citySelect = $("#d_city");
	var resultHidInp = $("#result_ab");
	var getLngLat = function(){
		var city = citySelect.find("option:selected").text();
		var address = addressInp.val();
		LngLat.getByAddress(city,address,{
			loading : function(){
				console && console.log("city="+city+" address="+address);
				console && console.log("loading:正在获取经纬度");
			},
			success : function(point){
				console && console.log("success:"+point.lng+","+point.lat);
				resultHidInp.val(point.lng+","+point.lat);
			},
			fail : function(){
				console && console.log("complete:获取经纬度失败");
				resultHidInp.val("0,0");
			},
			error : function(){
				console && console.log("参数缺省city或address");
			}
		})
	};
	addressInp.on("blur",getLngLat);
	citySelect.on("change",getLngLat);
	//$("#saveProduct").on("click",function(e){
	//	getLngLat();
	//	$("#mainForm").submit();
	//})
})

